package com.example.administrator.jingpingou.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.bean.ThemeBean;

import java.util.List;

/**
 * Created by lenovo on 2016/8/29.
 */
public class ThemeAdapter extends BaseAdapter {
    private Context mContext;
    private List<ThemeBean> mData;
    public ThemeAdapter(List<ThemeBean> data,Context context){
        mData =data;
//        Log.d("tag", "ThemeAdapter: =====ThemeAdapter"+mData);
        mContext=context;
    }
    @Override
    public int getCount() {
        return mData==null?0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_fragment_theme,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        ThemeBean themeBean = mData.get(position);
        Log.d("tag", "getView: themeBean========="+themeBean);
        holder.mTexyViewTitle.setText(themeBean.getTitle());
//        Log.d("tag", "getView: =====themeBean.getTitle()"+themeBean.getTitle());

        RequestQueue queue = Volley.newRequestQueue(mContext);
        ImageRequest imageRequest = new ImageRequest(themeBean.getIphoneimg(), new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                holder.mImageViewIcon.setImageBitmap(response);
            }
        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(imageRequest);
        return convertView;
    }
    public static class ViewHolder{
        private TextView mTexyViewTitle;
        private ImageView mImageViewIcon;
        public ViewHolder(View view){
            mTexyViewTitle = (TextView) view.findViewById(R.id.tv_item_fragment_theme_title);
            mImageViewIcon = (ImageView) view.findViewById(R.id.iv_item_fra_theme);
        }
    }
}
